import { contextBridge, ipcRenderer } from 'electron'

// 暴露安全的API给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  getAppVersion: () => ipcRenderer.invoke('get-app-version'),
  getAppName: () => ipcRenderer.invoke('get-app-name'),

  // 平台信息
  platform: process.platform,

  // 应用控制
  minimize: () => ipcRenderer.invoke('window-minimize'),
  maximize: () => ipcRenderer.invoke('window-maximize'),
  close: () => ipcRenderer.invoke('window-close'),

  // 网络请求
  fetch: (url: string, options: any) => ipcRenderer.invoke('fetch', url, options),

  // 配置存储服务
  config: {
    getAll: () => ipcRenderer.invoke('config:get-all'),
    get: (key: string) => ipcRenderer.invoke('config:get', key),
    set: (key: string, value: any) => ipcRenderer.invoke('config:set', key, value),
    getAI: () => ipcRenderer.invoke('config:get-ai'),
    setAI: (config: any) => ipcRenderer.invoke('config:set-ai', config),
    getEditor: () => ipcRenderer.invoke('config:get-editor'),
    setEditor: (config: any) => ipcRenderer.invoke('config:set-editor', config),
    addRecentDocument: (documentId: string) => ipcRenderer.invoke('config:add-recent-document', documentId),
    getRecentDocuments: () => ipcRenderer.invoke('config:get-recent-documents'),
    reset: () => ipcRenderer.invoke('config:reset')
  }
})

// 类型声明
declare global {
  interface Window {
    electronAPI: {
      getAppVersion: () => Promise<string>
      getAppName: () => Promise<string>
      platform: string
      minimize: () => Promise<void>
      maximize: () => Promise<void>
      close: () => Promise<void>
      fetch: (url: string, options: any) => Promise<any>
      config: {
        getAll: () => Promise<any>
        get: (key: string) => Promise<any>
        set: (key: string, value: any) => Promise<boolean>
        getAI: () => Promise<any>
        setAI: (config: any) => Promise<boolean>
        getEditor: () => Promise<any>
        setEditor: (config: any) => Promise<boolean>
        addRecentDocument: (documentId: string) => Promise<boolean>
        getRecentDocuments: () => Promise<string[]>
        reset: () => Promise<boolean>
      }
    }
  }
}